<template>
  <div class="acceptance">
    <editForm
      :formConfig="acceptConfig"
      ref="acceptForm"
      :formData="acceptData"
    ></editForm>
    <div class="attachConten" v-if="orderType != 'defect'">
      <div class="attachTitle">
        <span class="redPoint" v-if="!isNoRequired">*</span> 附件:
      </div>
      <div class="tebleDiv">
        <attachFormNew
          style="width: 60%"
          :disabled="baseAttachBtn"
          :attachSetting="attachSetting"
          isDelete
          multiple
          ref="attachFile"
        ></attachFormNew>
      </div>
    </div>
  </div>
</template>
<script>
import {
  acceptUpdateApi, //更新-工单验收
  acceptDetailsOrder, //获取详情-工单验收(orderId)
} from "@/api/orderManagement/common";
export default {
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    orderType: {
      type: String,
      default: null,
    },
    pageType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      /* 验收信息 */
      acceptConfig: {
        itemArr: [
          {
            type: "select",
            label: "验收结果",
            prop: "result",
            required: true,
            selectOption: [
              {
                name: "通过",
                label: "通过",
                value: "1",
              },
              {
                name: "不通过",
                label: "不通过",
                value: "0",
              },
            ],
            methods: (obj) => {
              if (this.orderType == "defect") {
                if (obj.value == "1") {
                  this.acceptConfig.itemArr[1].isHide = true;
                } else {
                  this.acceptConfig.itemArr[1].isHide = false;
                }
              }
            },
            span: 8,
          },
          {
            type: "select",
            label: "驳回节点",
            prop: "status",
            required: true,
            isHide: true,
            selectOption: [
              {
                name: "待派工",
                label: "待派工",
                value: "WAIT_DISPATCH",
              },
              {
                name: "执行中",
                label: "执行中",
                value: "IN_EXECUTE",
              },
            ],
            methods: (obj) => {},
            span: 8,
          },
          {
            type: "select",
            label: "服务质量",
            prop: "level",
            required: true,
            selectOption: this.$store.getters["dirtData"](
              "order_service_level"
            ),
            formatter: (val) => {
              return this.$store.getters["fineDirtDataName"](
                "order_service_level",
                val
              );
            },
            methods: (obj) => {},
            span: 8,
          },
          {
            label: "现场验收人",
            prop: "acceptorName", //多选时，字段要为数组类型
            propArr: "acceptorArr",
            idsStr: "acceptor",
            multiple: false, //是否多选
            isOutOrg: 0, //是否是外单位
            allowCreate: true, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            placeholder: "",
            methods: (obj) => {},
            required: true,
            span: 8,
          },

          {
            label: "现场验收时间",
            prop: "acceptTime",
            type: "datetime",
            required: true,
            // pickerOptions: {
            //   disabledDate(time) {
            //     return time.getTime() < Date.now() - 3600 * 1000 * 24;
            //   }
            // },
            span: 8,
          },
          {
            label: "验收意见",
            prop: "opinion",
            type: "textarea",
            maxlength: "100",
            span: 8,
            rows: 2,
          },
        ],
      },
      acceptData: {},

      //附件设置
      attachSetting: {
        businessId: "", //单据的ID
        businessType: "acceptFile",
      },
      isGetData: false,
    };
  },
  watch: {
    "formData.status": {
      handler(val) {
        //更新数据
        this.init();
      },
      deep: true,
    },
    pageType: {
      handler(val) {
        this.init();
      },
    },
  },
  computed: {
    baseAttachBtn() {
      if (this.formData.status == "WAIT_ACCEPT" && this.pageType != "detail") {
        return false;
      } else {
        return true;
      }
    },
    isNoRequired() {
      return (
        localStorage.getItem("currDs") == "water_10" &&
        ["maintain"].includes(this.orderType)
      );
    },
  },
  created() {
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      //是否可编辑
      let { id, status } = this.formData;
      //初始化附件ID
      this.attachSetting.businessId = id;
      if (status == "WAIT_ACCEPT" && this.pageType == "edit") {
        //待验收
        /* 初始化信息 */
        this.acceptData = {
          result: "1",
          level: "1",
          acceptTime: this.$utils.getCurrentDate("minutes"),
        };
        let itemArr = [];
        $.extend(true, itemArr, this.acceptConfig.itemArr);
        itemArr.map((i) => (i.disabled = false));
        this.acceptConfig.itemArr = itemArr;
      } else {
        //已归档
        let itemArr = [];
        $.extend(true, itemArr, this.acceptConfig.itemArr);
        itemArr.map((i) => (i.disabled = true));
        this.acceptConfig.itemArr = itemArr;
      }
      this.getDataJudge();
    },
    getDataJudge() {
      let { id, status } = this.formData;
      if (["WAIT_ACCEPT", "WAIT_REPORT", "ARCHIVED"].includes(status)) {
        this.getAcceptDetails();
      }
    },
    //获取验收信息的详情
    getAcceptDetails() {
      acceptDetailsOrder(this.formData.id).then((res) => {
        let data = res.data || {};
        if (JSON.stringify(data) != "{}") {
          this.$emit("setAcceptData", data);
          let option = this.$utils.getOptionData(
            data.acceptorName,
            data.acceptor,
            "0"
          );
          this.$refs.acceptForm.setItemOption("acceptorName", option);
          data.acceptorArr = data.acceptor;
          if (this.orderType == "defect") {
            if (data.result == "1") {
              this.acceptConfig.itemArr[1].isHide = true;
            } else {
              this.acceptConfig.itemArr[1].isHide = false;
            }
          }
          //设置发现人的Option
          setTimeout(() => {
            this.acceptData = data;
          }, 200);
        }
      });
    },
    async saveData(type) {
      let data = await this.$refs.acceptForm.getFormData();
      if (type == "save") {
        data.isCommit = false;
      } else if (type == "submit") {
        data.isCommit = true;
      }
      if (this.orderType != "defect") {
        let hasFile = this.$refs.attachFile.getFileListData();
        if (hasFile.length > 0 || this.isNoRequired) {
          data.orderId = this.formData.id;
          data.orderType = this.orderType;
          delete data.acceptorArr;
          acceptUpdateApi(data, this.orderType)
            .then((res) => {
              this.isGetData == false;
              this.saveAttachFile();
              this.$message.success("操作成功！");
              if (type == "submit") {
                if (data.result == "1") {
                  this.$utils.addOperateLog(this.formData.id, "验收通过");
                } else {
                  this.$utils.addOperateLog(
                    this.formData.id,
                    `验收不通过\n${data.opinion}`
                  );
                }
                this.$emit("changeData");
              } else {
                this.$utils.addOperateLog(this.formData.id, "保存-验收信息");
                this.getAcceptDetails();
              }
              return true;
            })
            .catch((e) => {
              return false;
            });
        } else {
          this.$message.warning("请上传相应附件！");
        }
      } else {
        data.orderId = this.formData.id;
        data.orderType = this.orderType;
        delete data.acceptorArr;
        acceptUpdateApi(data, this.orderType)
          .then((res) => {
            this.isGetData == false;
            this.$message.success("操作成功！");
            if (type == "submit") {
              if (data.result == "1") {
                this.$utils.addOperateLog(this.formData.id, "验收通过");
              } else {
                this.$utils.addOperateLog(
                  this.formData.id,
                  `验收不通过\n${data.opinion}`
                );
              }
              this.$emit("changeData");
            } else {
              this.$utils.addOperateLog(this.formData.id, "保存-验收信息");
              this.getAcceptDetails();
            }
            return true;
          })
          .catch((e) => {
            return false;
          });
      }
    },
    saveAttachFile() {
      this.$refs.attachFile.save();
    },
    getFormData() {
      return this.$refs.acceptForm.getCurrentData();
    },
  },
};
</script>
<style lang="less" scoped>
.attachConten {
  display: flex;
  flex-flow: wrap nowrap;
  .redPoint {
    color: red;
  }
  .attachTitle {
    width: 120px;
  }
  .tebleDiv {
    width: 100%;
  }
}
</style>
